/*============================================================================*/
/*       UNIVERSIDAD DE GUADALAJARA - CONTINENTAL AUTOMOTIVE SYSTEMS          */
/*============================================================================*/
/*                        OBJECT SPECIFICATION                                */
/*============================================================================*
* %name:            % Autolamp
* %version:         %
* %created_by:      % team 3
* %date_created:    % 17/oct/2012
*=============================================================================*/
/* DESCRIPTION : Autolamp control                                             */
/*============================================================================*/
/* FUNCTION COMMENT : Provices cotrol of lamps in automatic or manual mode    */
/*                    using a light sensor for automatic mode and a button    */
/*                    for manual mode                                         */
/*============================================================================*/

/* CREATING RAM variables */

/*  CREATING GLOBAL ENUMS*/

	enum 	// SYSTEM STATES
	{
		LightsOff, //off state
		HalfLights,
		LightsOn,
		InvalidState
	}SystemState;

	
	enum	// MODE STATE SELECTION
	{
		OFF,
		Automatic,
		Parklamps,
		Headlamps,
		InvalidSelection
	}ModeSelection;
	
	enum	// LOGIC STATES
	{
		CLEAR,
		SET
	}LogicState;

/* INCLUDING LIBRARIES */
#include "stdtypedef.h"
#include "p18f4550.h"
#include "core.c"
#include "hw_adcs.c"	


#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config FOSC = INTOSCIO_EC

void initport(void);
void SystemReset(void);
void ReadButton(void);
void ParkLights(T_UBYTE lub_ParkLampState);
void HeadLights(T_UBYTE lub_HeadLampState);
void ReadButton(void);


/**************************************************************
 *  Name                 : main
 *  Description          : main program, initialize cpu and run aplication
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void main(void)
{
	initcpu();
    initadc();
	initport();
	SystemReset();
	
	while(1)
	{
		switch (ModeSelection)	// SET MODE SELECTION LEDS
		{
			case OFF:
				PORTAbits.RA3 = 0;	// TURN OFF THE MODE0 LED
				PORTAbits.RA4 = 0;	// TURN OFF THE MODE1 LED
				SystemState = LightsOff;
				break;
			case Automatic:
				PORTAbits.RA3 = 1;	// TURN ON THE MODE0 LED
				PORTAbits.RA4 = 0;	// TURN OFF THE MODE1 LED
				break;
			case Parklamps:
				PORTAbits.RA3 = 0;	// TURN OFF THE MODE0 LED
				PORTAbits.RA4 = 1;	// TURN ON THE MODE1 LED
				SystemState = HalfLights;
				break;
			case Headlamps:
				PORTAbits.RA3 = 1;	// TURN ON THE MODE0 LED
				PORTAbits.RA4 = 1;	// TURN ON THE MODE1 LED
				SystemState = LightsOn;
				break;
			default:
				; // nothing
				break;
		}
			
		switch(SystemState)
		{
			case LightsOff:
				HeadLights(CLEAR);
				ParkLights(CLEAR);
				SystemState = LightsOff;
				break;
			
			case HalfLights:
				HeadLights(CLEAR);
				ParkLights(SET);
				SystemState = HalfLights;
				break;
			case LightsOn:
				HeadLights(SET);
				ParkLights(SET);
				SystemState = LightsOn;
				break;
				
			default:
				// nothing
				break;
			}
		ReadButton();
		if (ModeSelection == Automatic)
		{
			SystemState=LightMeasure();
		}
		else
		{
			;
		}
	}	
}

/**************************************************************
 *  Name                 : initport
 *  Description          : set port settings
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void initport(void)
{
	//TRISA=0X21;
	TRISAbits.TRISA0 = 1;	// ADC PIN
	TRISAbits.TRISA1 = 0;	// PARKLIGHTS LED
	TRISAbits.TRISA2 = 0;	// HEADLIGHTS LED
	TRISAbits.TRISA3 = 0;	// MODE 0 LED
	TRISAbits.TRISA4 = 0;	// MODE 1 LED
	TRISAbits.TRISA5 = 1;	// BUTTON PIN
	TRISAbits.TRISA6 = 0;	// NOT IN USE
	PORTA = 0X00;	// SET PORTA TO 0X00
}

/**************************************************************
 *  Name                 :  SystemReset
 *  Description          :  set global variables to default values
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
 
void SystemReset(void)
{
	SystemState = LightsOff;
	ModeSelection = Automatic;
}

/**************************************************************
 *  Name                 :  ReadButton
 *  Description          :  Check if mode selector button had been pushed 
 *					     :  and define new value for selector
 *  Parameters           :  none
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void ReadButton(void)
{
	if(PORTAbits.RA5)
	{
		T_UWORD luw_Counter;
		while(PORTAbits.RA5)
			{
				;
			}
		switch (ModeSelection)
		{
			case OFF:
				ModeSelection = Automatic;
				break;
			case Automatic:
				ModeSelection = Parklamps;
				break;
			case Parklamps:
				ModeSelection = Headlamps;
				break;
			case Headlamps:
				ModeSelection = OFF;
				break;
			default:
				//nothing
				break;				
		}
		
	}
	else
	{
		;
	}
}

/**************************************************************
 *  Name                 :  ParkLights
 *  Description          :  Turn on and turn off HeadLamps led 
 *  Parameters           :  A local unsigned byte variable (lub_ParkLampState) 
 *						 :  which values could be:
 *			   			 :  (ON) => Turn on ParkLamps
 *						 :	(OFF) => Turn off ParkLamps
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void ParkLights(T_UBYTE lub_ParkLampState)
{
	if(lub_ParkLampState)
	{
		PORTAbits.RA1 = 1;	// TURN ON THE PARKLAMPS LED
	}
	else
	{
		PORTAbits.RA1 = 0;	// TURN ON THE PARKLAMPS LED
	}
}

/**************************************************************
 *  Name                 :  HeadLights
 *  Description          :  Turn on and turn off HeadLamps led 
 *  Parameters           :  A local unsigned byte variable (lub_HeadLampState) which values could be:
 *			   			 :  (ON) => Turn on HeadLamps
 *						 :	(OFF) => Turn off the HeadLamps
 *  Return               :  none
 *  Critical/explanation :  No
 **************************************************************/
void HeadLights(T_UBYTE lub_HeadLampState)
{
	if(lub_HeadLampState)
	{
		PORTAbits.RA2 = 1;	// TURN ON THE HEADLAMPS LED
	}
	else
	{
		PORTAbits.RA2 = 0;	// TURN OFF THE HEADLAMPS LED
	}
}